﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace HYTools
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdAutoSave : IExternalCommand
	{
		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			Result result;
			try
			{
				if (cmdData == null)
				{
					throw new System.ArgumentNullException("commandData");
				}
				AutoSaveApp autoSaveApp = null;
				ExternalApplicationArray loadedApplications = cmdData.Application.LoadedApplications;
				for (int i = 0; i < loadedApplications.Size; i++)
				{
                    IExternalApplication externalApplication = loadedApplications.get_Item(i);
					try
					{
						AutoSaveApp autoSaveApp2 = (AutoSaveApp)externalApplication;
						if (autoSaveApp2 != null)
						{
							autoSaveApp = autoSaveApp2;
							break;
						}
					}
					catch (Exception ex)
					{
						string message = ex.Message;
					}
				}
				if (autoSaveApp == null)
				{
					msg = "未加载TestBIMApp";
					result = Autodesk.Revit.UI.Result.Failed;
				}
				else
				{
					AutoSaveForm autoSaveForm = new AutoSaveForm();
					if (autoSaveForm.ShowDialog() == DialogResult.OK)
					{
						autoSaveApp.SetAutoSaveMark(autoSaveForm.AutoSave);
						autoSaveApp.SetTimerInterval(autoSaveForm.Interval);
						autoSaveApp.SetFileCount(autoSaveForm.FileCount);
					}
					result = Autodesk.Revit.UI.Result.Succeeded;
				}
			}
			catch (Exception ex2)
			{
				msg = ex2.Message;
				result = Autodesk.Revit.UI.Result.Failed;
			}
			return result;
		}
	}
}
